/**
 * @file sendq.h
 * Declarations for send queue.
 */

/* Created by Laszlo Ashin <kodest@gmail.com> in 2oo8. */

#ifndef _SENDQ_H
#define _SENDQ_H	1

#include <sys/types.h>

/** Minimal size of a buffer in sendq. */
#define MIN_SENDQ_SIZE	256

/** Maximal size of a buffer in sendq. */
#define MAX_SENDQ_SIZE	65536

/** Structure of sendq. */
typedef struct {

	/** Size of the currently allocated buffer. */
	size_t size;

	/** Length of the data currently in the buffer. */
	size_t data_length;

	/** The buffer of this queue. */
	char *buffer;

} sendq_t;

/**
 * Initializes the given sendq structure.
 * @param this Pointer to the sendq structure to initialize.
 */
void init_sendq(sendq_t *this);

/**
 * Frees up the given sendq structure.
 * @param this Pointer to the sendq structure to free up.
 */
void free_sendq(sendq_t *this);

/**
 * Puts some data into the given sendq.
 * @param this Pointer to sendq to put data into.
 * @param data Data to put into the sendq.
 * @param length Length of the data to put into the sendq.
 */
void sendq_put(sendq_t *this, const char *data, size_t length);

#define sendq_length(this) ((this)->data_length)

/**
 * Send data from sendq to the specified file descriptor.
 * @param this Pointer to sendq.
 * @param fd File descriptor of the send socket.
 * @return Number of sent bytes or -1 on error.
 */
ssize_t sendq_send(sendq_t *this, int fd);

#endif /* _SENDQ_H */
